﻿using ArivisTests.Models;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;

namespace ArivisTests.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ConfigController : Controller
    {
        private ArivisTestsDBContext db = new ArivisTestsDBContext();

        //
        // GET: /Config/
        public ActionResult Index()
        {
            return View();
        }

        #region --- Results ---

        //
        // GET: /Config/Results
        public ActionResult Results()
        {
            InitKnownColors();
            return View("IndexResults", db.TestResults);
        }


        //
        // GET: /Config/AddResult
        public ActionResult AddResult()
        {
            InitKnownColors();
            return View("AddResult", new TestResult());
        }
        
        //
        // POST: /Config/AddResult
        [HttpPost]
        public ActionResult AddResult(TestResult testResult)
        {
            if (ModelState.IsValid)
            {
                db.TestResults.Add(testResult);
                db.SaveChanges();
            }
            return View("IndexResults", db.TestResults);
        }

        //
        // GET: /Config/EditResult/1
        public ActionResult EditResult(int id)
        {
            InitKnownColors();
            var testResult = db.TestResults.FirstOrDefault(x => x.Id == id);
            return View(testResult);
        }

        //
        // POST: /Config/EditResult
        [HttpPost]
        public ActionResult EditResult(TestResult testResult)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testResult).State = EntityState.Modified;
                db.SaveChanges();
            }
            return View("IndexResults", db.TestResults);
        }

        //
        // GET: /Config/DeleteResult
        public ActionResult DeleteResult(int id)
        {
            var testResult = db.TestResults.FirstOrDefault(x => x.Id == id);
            return View(testResult);
        }

        //
        // POST: /Config/DeleteResult
        [HttpPost, ActionName("DeleteResult")]
        public ActionResult DeleteResultConfirmed(int id)
        {
            var testResult = db.TestResults.FirstOrDefault(x => x.Id == id);
            db.TestResults.Remove(testResult);
            db.SaveChanges();
            return View("IndexResults", db.TestResults);
        }

        #endregion

        protected void InitKnownColors()
        {
            // We take only static property to avoid properties like Name, IsSystemColor ...
            PropertyInfo[] propInfos = typeof(System.Drawing.Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
            ViewBag.KnownColors = propInfos.Select(propInfo => propInfo.Name).Where(x => x != "Transparent").ToList();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
